﻿package mortal.game.view.copy.towerCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.copyFailWin.*;
    import mortal.game.view.copy.towerCopy.panel.*;
    import mortal.game.view.copy.towerCopy.win.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TowerCopyController extends CopyControllerBase
    {
        private var _towerProxy:TowerProxy;
        private var _towerModule:TowerModule;
        private var _dropTimer:SecTimer;
        private var _dropNum:int = 0;
        private var _rewardArr:Array;

        public function TowerCopyController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            this._towerProxy = GameProxy.tower;
            NetDispatcher.addCmdListener(ServerCommand.TowerPublicTowerPass2, this.loadPassedAndUpdateWin);
            NetDispatcher.addCmdListener(ServerCommand.TowerPublicTowerCopyInfo2, this.loadCurrentPassInfoAndUpdateWin);
            NetDispatcher.addCmdListener(ServerCommand.TowerPublicTowerTopListUpdate, this.loadRankListAndUpdateWin);
            NetDispatcher.addCmdListener(ServerCommand.TowerPublicTowerEnd2, this.loadTowerEndAndUpdateWin);
            NetDispatcher.addCmdListener(ServerCommand.TowerCopy2ScanEnd, this.onTowerCopy2ScanEnd);
            NetDispatcher.addCmdListener(ServerCommand.TowerScanStatusRes, this.onTowerScanStatusRes);
            NetDispatcher.addCmdListener(ServerCommand.TowerScanRewardSucceed, this.onTowerScanRewardSucceed);
            NetDispatcher.addCmdListener(ServerCommand.TowerCopy2EnterSucceed, this.onTowerCopy2EnterSucceed);
            NetDispatcher.addCmdListener(ServerCommand.TowerCopy2DropEnd, this.onTowerCopy2DropEnd);
            Dispatcher.addEventListener(EventName.TowerShow, this.onTowerShow);
            Dispatcher.addEventListener(EventName.TowerEnterCopy, this.towerEnterCopy);
            Dispatcher.addEventListener(EventName.TowerReset, this.onTowerReset);
            Dispatcher.addEventListener(EventName.TowerSweep, this.onTowerSweep);
            Dispatcher.addEventListener(EventName.TowerGetReward, this.onTowerGetReward);
            Dispatcher.addEventListener(EventName.TowerGetStatus, this.onTowerGetStatus);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.showIcon);
            return;
        }// end function

        private function showIcon(event:Event = null) : void
        {
            var _loc_2:* = Cache.instance.role.roleEntityInfo.entityInfo.level;
            if (_loc_2 >= GameDefConfig.instance.getSystemOpenLevel(11))
            {
                ClimbTowerIcon.instance.show();
            }
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            TowerCopyTaskView.instance.display();
            if (this._towerModule && !this._towerModule.isHide)
            {
                this._towerModule.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            TowerCopyTaskView.instance.hide();
            if (cache.copy.tower.towerCopyInfo.result == false)
            {
                CopyFailBaseWin.instance.show();
                CopyFailBaseWin.instance.tipsImageUrl = ImagesConst.copyFailBaseWinTxt_2;
            }
            else
            {
            }
            return;
        }// end function

        override protected function addListeners() : void
        {
            super.addListeners();
            return;
        }// end function

        override protected function removeListeners() : void
        {
            super.removeListeners();
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._towerModule == null)
            {
                this._towerModule = new TowerModule();
            }
            return this._towerModule;
        }// end function

        private function onTowerShow(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            return;
        }// end function

        public function get allowPassTo() : Boolean
        {
            var _loc_1:* = Cache.instance.copy.tower.towerCopyInfo;
            if (_loc_1 != null && !_loc_1.result)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71011));
                return false;
            }
            return true;
        }// end function

        private function loadRankListAndUpdateWin(param1:STowerTopListMsg) : void
        {
            Cache.instance.copy.tower.towerTopList = param1;
            TowerRankWin.instance.refresh();
            return;
        }// end function

        private function loadCurrentPassInfoAndUpdateWin(param1:STowerCopy2Info) : void
        {
            Cache.instance.copy.tower.towerCopyInfo = param1;
            Cache.instance.copy.tower.towerPassMsg = null;
            TowerCopyTaskView.instance.refresh();
            return;
        }// end function

        private function loadPassedAndUpdateWin(param1:STowerCopy2PassMsg) : void
        {
            Cache.instance.copy.tower.towerPassMsg = param1;
            TowerCopyTaskView.instance.refresh();
            TowerModule(this.view).refresh();
            this.stapDropTimer();
            return;
        }// end function

        private function stapDropTimer() : void
        {
            if (this._dropTimer == null)
            {
                this._dropTimer = new SecTimer(1);
            }
            this._dropNum = 0;
            this._dropTimer.reset();
            this._dropTimer.start();
            this._dropTimer.addListener(TimerType.ENTERFRAME, this.checkDropNumAndPopResultPanel);
            return;
        }// end function

        private function checkDropNumAndPopResultPanel(param1:SecTimer) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:String = this;
            var _loc_4:* = this._dropNum + 1;
            _loc_3._dropNum = _loc_4;
            if (this._dropNum < 10)
            {
                _loc_2 = TowerCopyUtil.isDropEmpty();
                if (_loc_2 && this._dropNum > 1)
                {
                    this.showResultPanel();
                }
            }
            else
            {
                this.showResultPanel();
            }
            return;
        }// end function

        private function showResultPanel() : void
        {
            this._dropTimer.stop();
            this._dropNum = 0;
            this._dropTimer.dispose();
            this._dropTimer = null;
            if (Cache.instance.copy && Cache.instance.copy.copyInfo)
            {
                TowerResultPanel.instance.show();
                TowerResultPanel.instance.showRewards(this._rewardArr.concat());
            }
            this._rewardArr = null;
            return;
        }// end function

        private function loadTowerEndAndUpdateWin(param1:Object) : void
        {
            MsgManager.showRollTipsMsg("" + Language.getString(71012));
            if (Cache.instance.copy.tower.getPlayerMaxPass(cache.copy.copyInfo.code) == GameConst.TConstTowerMaxLevel)
            {
                Alert.show("" + Language.getString(71013), null, Alert.OK);
            }
            else if (cache.copy.tower.towerCopyInfo && cache.copy.tower.towerCopyInfo.result == false)
            {
                CopyFailBaseWin.instance.show();
                CopyFailBaseWin.instance.tipsImageUrl = ImagesConst.copyFailBaseWinTxt_2;
            }
            else
            {
                TowerResultPanel.instance.show();
            }
            return;
        }// end function

        private function onTowerCopy2ScanEnd(param1:Object) : void
        {
            var _loc_2:* = param1 as STowerCopy2EndMsg;
            cache.copy.tower.setScanStatus(_loc_2.copyCode, 0);
            if (isViewShow)
            {
                this._towerModule.scanEnd();
            }
            return;
        }// end function

        private function onTowerScanStatusRes(param1:Object) : void
        {
            cache.copy.tower.setScanStatus(int(param1.copyCode), int(param1.lv));
            if (isViewShow)
            {
                this._towerModule.towerScanStatus = cache.copy.tower.getScanStatus(int(param1.copyCode));
            }
            return;
        }// end function

        private function onTowerScanRewardSucceed(param1:Object) : void
        {
            if (isViewShow)
            {
                this._towerModule.towerScanRewardSucceed();
            }
            if (TowerSweepBagPanel.isViewShow)
            {
                TowerSweepBagPanel.instance.hide();
            }
            return;
        }// end function

        private function onTowerCopy2EnterSucceed(param1:Object) : void
        {
            cache.copy.tower.isDropEnd = false;
            cache.copy.tower.dropItems.splice(0);
            if (TowerResultPanel.isViewShow)
            {
                TowerResultPanel.instance.hide();
            }
            return;
        }// end function

        private function onTowerCopy2DropEnd(param1:Object) : void
        {
            this._rewardArr = TowerResultPanel.instance.setReward();
            return;
        }// end function

        private function towerEnterCopy(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (Cache.instance.copy.isInCopy && !GameMapUtil.curMapState.isTowerMap && !GameMapUtil.curMapState.isCrossMap)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71014));
                return;
            }
            if (Cache.instance.copy.tower.getPlayerMaxPass(_loc_2) >= GameConst.TConstTowerMaxLevel)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71015));
                return;
            }
            var _loc_3:* = Cache.instance.copy.tower.getCurrentLevel(_loc_2);
            GameProxy.tower.enterTowerCopy(_loc_2, _loc_3);
            return;
        }// end function

        private function onTowerReset(event:DataEvent) : void
        {
            GameProxy.tower.resetTowerCopy(event.data as int);
            return;
        }// end function

        private function onTowerSweep(event:DataEvent) : void
        {
            GameProxy.tower.scanTowerCopy(event.data as int);
            return;
        }// end function

        private function onTowerGetReward(event:DataEvent) : void
        {
            GameProxy.tower.getTowerScanReward(event.data as int);
            return;
        }// end function

        private function onTowerGetStatus(event:DataEvent) : void
        {
            GameProxy.tower.getTowerScanStatus(event.data as int);
            return;
        }// end function

    }
}
